#!/bin/sh

jobname="toppage_print"

infol="入力フォルダ"
    if ! [ -d "$infol" ] ; then echo "input folder not exist: $infol" ; exit ; fi

wrkfol="作業フォルダ"
    if ! [ -d "$wrkfol" ] ; then echo "work folder not exist: $wrkfol" ; exit ; fi

printer="プリンタ名"

flagfol="実行中フラグフォルダ"
    if ! [ -d "$flagfol" ] ; then echo "flag folder not exist: $flagfol" ; exit ; fi

executing="$flagfol/$jobname.run"
    if [ -f "$executing" ] ; then exit ; fi

infiles=$( find "$infol" -name *.pdf 2>/dev/null )
    if [ -z "$infiles" ] ; then exit ; fi

# start
    touch "$executing"
    date +"$jobname ( %Y-%m-%d %H:%M:%S )"
    for fn in $infiles ; do
        name=$( echo "$fn" | perl -pe ' s,^.*/,, ; s,[.]pdf$,,i ' )
        sh "$jobname/$jobname.sh" "$infol" "$name" "$wrkfol" "$printer"
        mv "$infol/$name.pdf" "$infol/.."
    done
    echo "  ."
    rm "$executing"

